#include "StdAfx.h"
#include <QObject>
#include "appsettings.h"
#include <QSettings>

////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void AppSettings::setValue( const QString & valueName, const QVariant & value )
{
    QSettings settings( COMPANY, SOFTWARE );
    settings.setValue( valueName, value );
}

QVariant AppSettings::value( const QString & valueName )
{
    QSettings settings( COMPANY, SOFTWARE );
    return settings.value( valueName, QVariant() );
}

void AppSettings::saveWidgetState( const QWidget & widget, const QString & widgetName )
{
    QString dlgSize("size");
    QString dlgPos("pos");
    dlgSize.append( widgetName );
    dlgPos.append( widgetName );

    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "Dialogs" );
    settings.setValue( dlgSize, widget.size() );
    settings.setValue( dlgPos, widget.pos() );
    settings.endGroup();
}

void AppSettings::restoreWidgetState( QWidget & widget, const QString & widgetName )
{
    QString dlgSize("size");
    QString dlgPos("pos");
    dlgSize.append( widgetName );
    dlgPos.append( widgetName );

    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "Dialogs" );
    QVariant sizeVar = settings.value( dlgSize );
    QVariant posVar = settings.value( dlgPos );
    settings.endGroup();

    if (sizeVar.isValid())
    {
        widget.resize( sizeVar.toSize() );
    }

    if (posVar.isValid())
    {
        widget.move( posVar.toPoint() );
    }  
}

void AppSettings::saveHeaderViewState( const QHeaderView & headerView, const QString & headerViewName )
{
    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "Headers" );
    settings.setValue( headerViewName, headerView.saveState() );
    settings.endGroup();
}

void AppSettings::restoreHeaderViewState( QHeaderView & headerView, const QString & headerViewName )
{
    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "Headers" );
    QVariant value = settings.value( headerViewName );
    if (value.isValid())
        headerView.restoreState( value.toByteArray() );
    else
        headerView.setSortIndicator ( 0, Qt::AscendingOrder );
    settings.endGroup();
}

void AppSettings::saveOpenFileDialogPath( const QString & openFileDialogName, const QString & filePath )
{
    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "FilePaths" );
    settings.setValue( openFileDialogName, filePath );
    settings.endGroup();
}

QString AppSettings::restoreOpenFileDialogPath( const QString & openFileDialogName )
{
    QSettings settings( COMPANY, SOFTWARE );
    settings.beginGroup( "FilePaths" );
    QString filePath = settings.value( openFileDialogName, QDir::currentPath() ).toString();
    settings.endGroup();
    return filePath;
}
